using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DataAccess;
using System.Collections;
using System.Reflection;
namespace Business
{
    public class bBase
    {
        public static object GetObject(string SPName,CommandType type, IList<SqlParameter> paramList)
        {
            object obj = null;
            DBHelper helper = new DBHelper();
            // Build parameter list
            try
            {
                obj = helper.ExecuteScalar(SPName,type, paramList);
            }
            catch (Exception)
            {
                obj = null;
            }
            return obj;
        }

        public static IList LoadQueryString(Type t, string SPName,CommandType type, IList<SqlParameter> paramList)
        {
            //Type t = obj.GetType();
            PropertyInfo[] pi = t.GetProperties();
            DBHelper helper = new DBHelper();
            SqlDataReader dr = helper.ExecuteQuery(SPName,type, paramList);
            //SqlDataReader dr = cmd.ExecuteReader();
            IList il = new ArrayList();
            while (dr.Read())
            {
                object obj1 = Activator.CreateInstance(t);
                foreach (PropertyInfo p in pi)
                {
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        if (p.Name.Equals(dr.GetName(i)) && p.Name != null)
                        {
                            try
                            {
                                p.SetValue(obj1, Convert.ChangeType(dr[p.Name], p.PropertyType), null);
                            }
                            catch (Exception )
                            {
                                p.SetValue(obj1, p.GetValue(obj1, null), null);

                            }
                        }
                    }
                }
                il.Add(obj1);
            }
            dr.Close();
            return il;
        }
    }
}
